<?php

$num=1;
$childs=[];


$dataNum=1;

pcntl_signal(SIGUSR1,function($signo){

    echo posix_getpid()."---".$signo.PHP_EOL;
});
swoole_set_process_name("my-master-".posix_getpid());


for ($i=0;$i<$num;$i++){

    $p=new swoole_process(function(swoole_process $worker) use ($i){
        callBack($worker,$i);
    },false);

    $pid=$p->start();

    $childs[$pid]=$p;
    $p->write($dataNum);
}


function callBack(swoole_process $worker,$i){
    $name="my-".$i."-".posix_getpid();
    swoole_set_process_name($name);

    $GLOBALS['worker'] = $worker;
    swoole_event_add($worker->pipe, function($pipe) {
        $worker = $GLOBALS['worker'];
        $dataNum=$worker->read();
        $dataNum=intval($dataNum)+1;
        $worker->write($dataNum);
        if($dataNum>5){
            $worker->exit();
        }

    });

}


while($dataNum=$p->read()){
    $dataNum=intval($dataNum);
    echo $dataNum.PHP_EOL;
    sleep(1);
    $p->write($dataNum);
}



while($childs){
    $r=\Swoole\Process::wait(false);
    $pid=$r['pid'];
    echo $pid." exit by signal :".$r['signal']."--code=".$r['code'].PHP_EOL;
    unset($childs[$pid]);
}










